﻿using System.Web;
using System.Web.Mvc;

namespace Songhay.Web.Mvc.Extensions
{
    /// <summary>
    /// Adds Extension Methods to <see cref="System.Web.Mvc.UrlHelper"/>.
    /// </summary>
    public static partial class UrlHelperExtensions
    {
        /// <summary>
        /// Gets the base URI.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="protocol">The URI protocol (e.g. HTTP).</param>
        /// <returns>
        ///     Returns the base URI with the following form:
        ///     {protocol}://{ServerNameAndPort()}/
        /// </returns>
        public static string BaseUri(this UrlHelper helper, string protocol)
        {
            return string.Concat(
                protocol,
                "://",
                UrlHelperExtensions.ServerNameAndPort(helper),
                "/"
            );
        }

        /// <summary>
        /// Gets the base URI Path ("~/" tilde output).
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <returns>
        ///     Returns the base URI path with the following form:
        ///     /{path}/
        /// </returns>
        public static string BaseUriPath(this UrlHelper helper)
        {
            return helper.Content("~/");
        }

        /// <summary>
        /// Gets the base URI Path ("~/" tilde output)
        /// with the appended “sub” path.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="subPath">The “sub” path to append to the base URI path.</param>
        /// <returns>
        /// Returns the base URI path with the following form:
        /// /{path}/{subPath}/
        /// </returns>
        public static string BaseUriPath(this UrlHelper helper, string subPath)
        {
            if(string.IsNullOrEmpty(subPath)) return UrlHelperExtensions.BaseUriPath(helper);
            if(subPath.StartsWith("./")) subPath = subPath.Remove(0, 2);
            if(subPath.StartsWith("/")) subPath = subPath.Remove(0, 1);
            if(!subPath.EndsWith("/")) subPath = string.Concat(subPath, "/");
            return string.Concat(helper.Content("~/"), subPath);
        }

        /// <summary>
        /// Gets the URI server name and port.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <returns>Returns URI parts from the current <see cref="System.Web.HttpContext"/>.</returns>
        public static string ServerNameAndPort(this UrlHelper helper)
        {
            return string.Concat(
                HttpContext.Current.Request.ServerVariables["SERVER_NAME"],
                ":",
                HttpContext.Current.Request.ServerVariables["SERVER_PORT"]
            );
        }

        /// <summary>
        /// Gets the URI server name and port.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="protocol">The URI protocol (e.g. HTTP).</param>
        /// <param name="path">The URI path after server name and port.</param>
        /// <returns>
        ///     Returns a complete URI with the following form:
        ///     {protocol}://{ServerNameAndPort()}/{path}
        /// </returns>
        public static string ServerNameAndPort(this UrlHelper helper, string protocol, string path)
        {
            if(!string.IsNullOrEmpty(path) && !path.StartsWith("/")) path = string.Concat("/", path);

            return string.Concat(
                protocol,
                "://",
                UrlHelperExtensions.ServerNameAndPort(helper),
                path
            );
        }

        /// <summary>
        /// Returns the path to the regular or minified version
        /// of the specified script file.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="scriptFileName">Name of the script file.</param>
        /// <remarks>
        /// This member is based on “ASP.NET MVC3 Razor Script Loading Tips”
        /// by Kazi Manzur Rashid
        /// [http://kazimanzurrashid.com/posts/asp-dot-net-mvc3-razor-script-loading-tips]
        /// </remarks>
        public static string Script(this UrlHelper helper, string scriptFileName)
        {
            const string path = "~/Scripts/";

            var content = string.Concat(path,
                scriptFileName,
                (helper.RequestContext.HttpContext.IsDebuggingEnabled ? ".js" : ".min.js"));

            return helper.Content(content);
        }
    }
}
